package org.ruixue.controller;

import cn.rkylin.oms.common.dto.DBLog;
import com.alibaba.fastjson.JSON;
import org.ruixue.response.BaseResponse;
import org.ruixue.service.OrderService;
import org.ruixue.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
@RequestMapping("/order")
public class OrderController {

	@Autowired
	OrderService orderService;

	@Autowired
	LogUtil logUtil;

	/**
	 * http://localhost:40005/microservice-yiyouyayin/order/downloadYiOrderWhole
	 * 获取全部订单（一个月内的订单）
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/downloadYiOrderWhole", method = RequestMethod.GET)
	private ResponseEntity<BaseResponse> downloadYiOrderWhole() {
		BaseResponse response = new BaseResponse();
		try {
			response = orderService.downloadYiOrderWhole();
			logUtil.info(new DBLog("", "", "downloadYiOrderWhole", JSON.toJSONString(response)));
		} catch (Exception e) {
			e.printStackTrace();
			response.setMsg(e.getMessage());
			response.setSuccess("false");
			logUtil.error(new DBLog("", "", "downloadYiOrderWhole", e.getMessage()), e);
		}
		return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
	}
	
	/**
	 * http://localhost:40005/microservice-yiyouyayin/order/downloadYiOrder?shopId=55ed507f052747a0aa97d56d63f53f99
	 * 根据网店获取订单（三个月内的订单）
	 * 
	 * @param shopId
	 *            网店ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/downloadYiOrder", method = RequestMethod.GET)
	private ResponseEntity<BaseResponse> downloadYiOrder(@RequestParam String shopId) {
		BaseResponse response = new BaseResponse();
		try {
			response = orderService.downloadYiOrder(shopId);
			logUtil.info(new DBLog(shopId, "", "downloadYiOrder", JSON.toJSONString(response)));
		} catch (Exception e) {
			e.printStackTrace();
			response.setMsg(e.getMessage());
			response.setSuccess("false");
			logUtil.error(new DBLog(shopId, "", "downloadYiOrder", e.getMessage()), e);
		}
		return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
	}
	
	/**
     * http://localhost:40005/microservice-yiyouyayin/order/downloadYiOrderByEcTradeCode?shopId=55ed507f052747a0aa97d56d63f53f99&ecTradeCode=2018022710041084530972503
     * 订单按订单号下载
     * 
	 * @param shopId
	 * @param ecTradeCode
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/downloadYiOrderByEcTradeCode", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> downloadYiOrderByEcTradeCode(@RequestParam String shopId, @RequestParam("ecTradeCode") String ecTradeCode) {
        BaseResponse response = new BaseResponse();
        try {
            response = orderService.downloadYiOrderByEcTradeCode(shopId, ecTradeCode);
            logUtil.info(new DBLog(shopId, "", "downloadYiOrderByEcTradeCode", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "downloadYiOrderByEcTradeCode", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }
}
